package com.example.gcmtest1;

import android.os.Bundle;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.support.v4.app.NavUtils;
import android.support.v4.app.NotificationCompat;

import com.google.android.gcm.GCMRegistrar;

public class MainActivity extends Activity implements View.OnClickListener {

	public static boolean IS_LOADED = false; 
	private String logName = "MainActivity";
	
	private Button cmdRegister, cmdUnregister;
	private TextView lblRegId;
	private EditText txtRegId;
	
	
	
    @Override
    public void onCreate(Bundle savedInstanceState) {    	
        super.onCreate(savedInstanceState);
        IS_LOADED = true;
        Log.d(logName, "onCreated");
        setContentView(R.layout.activity_main);
        
        MainActivityReceiver mr = new MainActivityReceiver(this);
        IntentFilter intentFiler1 = new IntentFilter("com.example.gcmtest1.intent.action.register.TEST");
        IntentFilter intentFiler2 = new IntentFilter("com.example.gcmtest1.intent.action.message.TEST");

        this.registerReceiver(mr, intentFiler1);
        this.registerReceiver(mr, intentFiler2);
                
        this.setupListener();
        
        GCMRegistrar.checkDevice(this);
        GCMRegistrar.checkManifest(this);
        
        String regId = GCMRegistrar.getRegistrationId(this);
        if (regId.equals("")) {
          GCMRegistrar.register(this, "518191396700");
          Log.d(logName, "just registered");
          
          regId = GCMRegistrar.getRegistrationId(this);
          this.txtRegId.setText(regId);
          this.lblRegId.setText("Just Registered");
        } else {
          Log.d(logName, "Already registered, regId:" + regId);
          this.txtRegId.setText(regId);
          this.lblRegId.setText("Registered");
        }
                
    }
    
    private void setupListener() {
    	this.cmdRegister = (Button) findViewById(R.id.cmdRegister);
    	this.cmdRegister.setOnClickListener(this);
    	
    	this.cmdUnregister = (Button) findViewById(R.id.cmdUnregister);
    	this.cmdUnregister.setOnClickListener(this);
    	
    	this.lblRegId = (TextView) findViewById(R.id.lblRegId);
    	this.txtRegId = (EditText) findViewById(R.id.txtRegId);
    	
    }
    
    public void setRegId(String regId) {
    	this.txtRegId.setText(regId);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

	@Override
	public void onClick(View v) {
		if (v == this.cmdRegister ) {
			Log.d(logName, "onClick:cmdRegister");
			String regId = GCMRegistrar.getRegistrationId(this);
			if (regId.equals("")) {
				// not register yet, going to register now
				GCMRegistrar.register(this, "518191396700");
		        Log.d(logName, "just registered");
		          
		        regId = GCMRegistrar.getRegistrationId(this);
		        this.txtRegId.setText(regId);
		        this.lblRegId.setText("Just Registered");
			} else {
				this.lblRegId.setText("Registered!!");
				this.txtRegId.setText(regId);
			}
			
			NotificationManager nm = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
			NotificationCompat.Builder n = new NotificationCompat.Builder(this);
			n.setSmallIcon(R.drawable.ic_launcher);
			n.setContentTitle("New Message");
			n.setContentText("XX");
			
			Intent ii = new Intent(this, MainActivity.class);
			PendingIntent i = PendingIntent.getActivity(this, 0, ii, 0);
			n.setContentIntent(i);
			
			Notification nn = n.build();   
			nm.notify(0, nn);		
			
			Log.d(logName, "Notified!!");
			
		} else if (v == this.cmdUnregister) {
			Log.d(logName, "onClick:cmdUnregister");
			GCMRegistrar.unregister(this);
			this.lblRegId.setText("Just unregistered");
			this.txtRegId.setText("");
		}
		
		
	}

    
}
